from lib.action import api
import sys
from machine import Timer

tim0 = Timer(0)


def event_loop():
    """事件循环"""
    print("start loop")
    api.get_poem_action()
    api.get_ws2812_action()
    api.get_tts_action(True)
    # api.fetch_tts_audio('韩小信')
    print("end loop")


def start_event_loop(loop=False):
    try:
        if loop:
            tim0.init(
                period=10 * 1000, mode=Timer.PERIODIC, callback=lambda t: event_loop()
            )
        else:
            event_loop()
    except Exception as e:
        sys.print_exception(e)


if __name__ == "__main__":
    import lib.net.wifi as wifi

    wifi.do_connect_wifi(False)
    start_event_loop(False)
